namespace HexLib
{
    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Globalization;
    using System.Windows.Forms;

    public class Office11Renderer : AbstractRenderer
    {
        private Font minuteFont;

        protected override void Dispose(bool mainThread)
        {
            base.Dispose(mainThread);
            if (this.minuteFont != null)
            {
                this.minuteFont.Dispose();
            }
        }

        public override void DrawAppointment(Graphics g, Rectangle rect, Appointment appointment, bool isSelected, Rectangle gripRect)
        {
            if (appointment == null)
            {
                throw new ArgumentNullException("appointment");
            }
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }
            if ((rect.Width != 0) && (rect.Height != 0))
            {
                using (StringFormat format = new StringFormat())
                {
                    format.Alignment = StringAlignment.Near;
                    format.LineAlignment = StringAlignment.Near;
                    if (appointment.Locked && isSelected)
                    {
                        using (Brush brush = new HatchBrush(HatchStyle.Wave, Color.LightGray, appointment.Color))
                        {
                            g.FillRectangle(brush, rect);
                            goto Label_009B;
                        }
                    }
                    using (SolidBrush brush2 = new SolidBrush(appointment.Color))
                    {
                        g.FillRectangle(brush2, rect);
                    }
                Label_009B:
                    if (isSelected)
                    {
                        using (Pen pen = new Pen(appointment.BorderColor, 4f))
                        {
                            g.DrawRectangle(pen, rect);
                        }
                        Rectangle rectangle = rect;
                        rectangle.Inflate(2, 2);
                        using (Pen pen2 = new Pen(SystemColors.WindowFrame, 1f))
                        {
                            g.DrawRectangle(pen2, rectangle);
                        }
                        rectangle.Inflate(-4, -4);
                        using (Pen pen3 = new Pen(SystemColors.WindowFrame, 1f))
                        {
                            g.DrawRectangle(pen3, rectangle);
                            goto Label_018C;
                        }
                    }
                    gripRect.Width++;
                    using (SolidBrush brush3 = new SolidBrush(appointment.BorderColor))
                    {
                        g.FillRectangle(brush3, gripRect);
                    }
                    using (Pen pen4 = new Pen(SystemColors.WindowFrame, 1f))
                    {
                        g.DrawRectangle(pen4, rect);
                    }
                Label_018C:
                    rect.X += gripRect.Width;
                    if (appointment.Alert)
                    {
                        Rectangle rectIcon = new Rectangle(rect.X, rect.Y, 24, 24);
                        g.DrawIcon(new Icon(GetType(), "DayView.Image.alert.ico"), rectIcon);
                        rect.X += rectIcon.Width;
                    }
                    g.DrawString(appointment.Title, this.BaseFont, SystemBrushes.WindowText, rect, format);
                }
            }
        }

        public override void DrawDayBackground(Graphics g, Rectangle rect)
        {
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }
            using (Brush brush = new SolidBrush(this.HourColor))
            {
                g.FillRectangle(brush, rect);
            }
        }

        public override void DrawDayHeader(Graphics g, Rectangle rect, DateTime date)
        {
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }
            using (StringFormat format = new StringFormat())
            {
                format.Alignment = StringAlignment.Center;
                format.FormatFlags = StringFormatFlags.NoWrap;
                format.LineAlignment = StringAlignment.Center;
                ControlPaint.DrawButton(g, rect, ButtonState.Inactive);
                ControlPaint.DrawBorder3D(g, rect, Border3DStyle.Etched);
                g.DrawString(CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(date.DayOfWeek), this.BaseFont, SystemBrushes.WindowText, rect, format);
            }
        }

        public override void DrawHourLabel(Graphics g, Rectangle rect, int hour, bool ampm)
        {
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }
            using (Pen pen = new Pen(ControlPaint.Light(ControlPaint.LightLight(SystemColors.WindowFrame))))
            {
                g.DrawLine(pen, rect.Left, rect.Y, rect.Width, rect.Y);
            }
            g.DrawString(hour.ToString("##00", CultureInfo.InvariantCulture), this.HourFont, SystemBrushes.ControlText, rect);
            rect.X += 0x1b;
            g.DrawString("00", this.MinuteFont, SystemBrushes.ControlText, rect);
        }

        public override Font MinuteFont
        {
            get
            {
                if (this.minuteFont == null)
                {
                    this.minuteFont = new Font(this.BaseFont, FontStyle.Italic);
                }
                return this.minuteFont;
            }
        }
    }
}
